{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# On finding the right topologies for a given reaction\n",
    "\n",
    "[Molecular dimensions of adsorptives](http://pubs.acs.org/doi/abs/10.1021/ja973906m)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "    <div class=\"bk-root\">\n",
       "        <a href=\"http://bokeh.pydata.org\" target=\"_blank\" class=\"bk-logo bk-logo-small bk-logo-notebook\"></a>\n",
       "        <span id=\"3f029d3b-28ca-428b-8cdd-7eb2cb5d4f64\">Loading BokehJS ...</span>\n",
       "    </div>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/javascript": [
       "\n",
       "(function(global) {\n",
       "  function now() {\n",
       "    return new Date();\n",
       "  }\n",
       "\n",
       "  var force = true;\n",
       "\n",
       "  if (typeof (window._bokeh_onload_callbacks) === \"undefined\" || force === true) {\n",
       "    window._bokeh_onload_callbacks = [];\n",
       "    window._bokeh_is_loading = undefined;\n",
       "  }\n",
       "\n",
       "\n",
       "  \n",
       "  if (typeof (window._bokeh_timeout) === \"undefined\" || force === true) {\n",
       "    window._bokeh_timeout = Date.now() + 5000;\n",
       "    window._bokeh_failed_load = false;\n",
       "  }\n",
       "\n",
       "  var NB_LOAD_WARNING = {'data': {'text/html':\n",
       "     \"<div style='background-color: #fdd'>\\n\"+\n",
       "     \"<p>\\n\"+\n",
       "     \"BokehJS does not appear to have successfully loaded. If loading BokehJS from CDN, this \\n\"+\n",
       "     \"may be due to a slow or bad network connection. Possible fixes:\\n\"+\n",
       "     \"</p>\\n\"+\n",
       "     \"<ul>\\n\"+\n",
       "     \"<li>re-rerun `output_notebook()` to attempt to load from CDN again, or</li>\\n\"+\n",
       "     \"<li>use INLINE resources instead, as so:</li>\\n\"+\n",
       "     \"</ul>\\n\"+\n",
       "     \"<code>\\n\"+\n",
       "     \"from bokeh.resources import INLINE\\n\"+\n",
       "     \"output_notebook(resources=INLINE)\\n\"+\n",
       "     \"</code>\\n\"+\n",
       "     \"</div>\"}};\n",
       "\n",
       "  function display_loaded() {\n",
       "    if (window.Bokeh !== undefined) {\n",
       "      var el = document.getElementById(\"3f029d3b-28ca-428b-8cdd-7eb2cb5d4f64\");\n",
       "      el.textContent = \"BokehJS \" + Bokeh.version + \" successfully loaded.\";\n",
       "    } else if (Date.now() < window._bokeh_timeout) {\n",
       "      setTimeout(display_loaded, 100)\n",
       "    }\n",
       "  }\n",
       "\n",
       "  function run_callbacks() {\n",
       "    window._bokeh_onload_callbacks.forEach(function(callback) { callback() });\n",
       "    delete window._bokeh_onload_callbacks\n",
       "    console.info(\"Bokeh: all callbacks have finished\");\n",
       "  }\n",
       "\n",
       "  function load_libs(js_urls, callback) {\n",
       "    window._bokeh_onload_callbacks.push(callback);\n",
       "    if (window._bokeh_is_loading > 0) {\n",
       "      console.log(\"Bokeh: BokehJS is being loaded, scheduling callback at\", now());\n",
       "      return null;\n",
       "    }\n",
       "    if (js_urls == null || js_urls.length === 0) {\n",
       "      run_callbacks();\n",
       "      return null;\n",
       "    }\n",
       "    console.log(\"Bokeh: BokehJS not loaded, scheduling load and callback at\", now());\n",
       "    window._bokeh_is_loading = js_urls.length;\n",
       "    for (var i = 0; i < js_urls.length; i++) {\n",
       "      var url = js_urls[i];\n",
       "      var s = document.createElement('script');\n",
       "      s.src = url;\n",
       "      s.async = false;\n",
       "      s.onreadystatechange = s.onload = function() {\n",
       "        window._bokeh_is_loading--;\n",
       "        if (window._bokeh_is_loading === 0) {\n",
       "          console.log(\"Bokeh: all BokehJS libraries loaded\");\n",
       "          run_callbacks()\n",
       "        }\n",
       "      };\n",
       "      s.onerror = function() {\n",
       "        console.warn(\"failed to load library \" + url);\n",
       "      };\n",
       "      console.log(\"Bokeh: injecting script tag for BokehJS library: \", url);\n",
       "      document.getElementsByTagName(\"head\")[0].appendChild(s);\n",
       "    }\n",
       "  };var element = document.getElementById(\"3f029d3b-28ca-428b-8cdd-7eb2cb5d4f64\");\n",
       "  if (element == null) {\n",
       "    console.log(\"Bokeh: ERROR: autoload.js configured with elementid '3f029d3b-28ca-428b-8cdd-7eb2cb5d4f64' but no matching script tag was found. \")\n",
       "    return false;\n",
       "  }\n",
       "\n",
       "  var js_urls = [\"https://cdn.pydata.org/bokeh/release/bokeh-0.12.5.min.js\", \"https://cdn.pydata.org/bokeh/release/bokeh-widgets-0.12.5.min.js\"];\n",
       "\n",
       "  var inline_js = [\n",
       "    function(Bokeh) {\n",
       "      Bokeh.set_log_level(\"info\");\n",
       "    },\n",
       "    \n",
       "    function(Bokeh) {\n",
       "      \n",
       "    },\n",
       "    \n",
       "    function(Bokeh) {\n",
       "      \n",
       "      document.getElementById(\"3f029d3b-28ca-428b-8cdd-7eb2cb5d4f64\").textContent = \"BokehJS is loading...\";\n",
       "    },\n",
       "    function(Bokeh) {\n",
       "      console.log(\"Bokeh: injecting CSS: https://cdn.pydata.org/bokeh/release/bokeh-0.12.5.min.css\");\n",
       "      Bokeh.embed.inject_css(\"https://cdn.pydata.org/bokeh/release/bokeh-0.12.5.min.css\");\n",
       "      console.log(\"Bokeh: injecting CSS: https://cdn.pydata.org/bokeh/release/bokeh-widgets-0.12.5.min.css\");\n",
       "      Bokeh.embed.inject_css(\"https://cdn.pydata.org/bokeh/release/bokeh-widgets-0.12.5.min.css\");\n",
       "    }\n",
       "  ];\n",
       "\n",
       "  function run_inline_js() {\n",
       "    \n",
       "    if ((window.Bokeh !== undefined) || (force === true)) {\n",
       "      for (var i = 0; i < inline_js.length; i++) {\n",
       "        inline_js[i](window.Bokeh);\n",
       "      }if (force === true) {\n",
       "        display_loaded();\n",
       "      }} else if (Date.now() < window._bokeh_timeout) {\n",
       "      setTimeout(run_inline_js, 100);\n",
       "    } else if (!window._bokeh_failed_load) {\n",
       "      console.log(\"Bokeh: BokehJS failed to load within specified timeout.\");\n",
       "      window._bokeh_failed_load = true;\n",
       "    } else if (force !== true) {\n",
       "      var cell = $(document.getElementById(\"3f029d3b-28ca-428b-8cdd-7eb2cb5d4f64\")).parents('.cell').data().cell;\n",
       "      cell.output_area.append_execute_result(NB_LOAD_WARNING)\n",
       "    }\n",
       "\n",
       "  }\n",
       "\n",
       "  if (window._bokeh_is_loading === 0) {\n",
       "    console.log(\"Bokeh: BokehJS loaded, going straight to plotting\");\n",
       "    run_inline_js();\n",
       "  } else {\n",
       "    load_libs(js_urls, function() {\n",
       "      console.log(\"Bokeh: BokehJS plotting callback run at\", now());\n",
       "      run_inline_js();\n",
       "    });\n",
       "  }\n",
       "}(this));"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import math\n",
    "import pandas as pd\n",
    "\n",
    "from bokeh.io import output_notebook\n",
    "from bokeh.plotting import figure, show, output_file\n",
    "from bokeh.charts import Bar\n",
    "from bokeh.charts.attributes import cat\n",
    "from bokeh.models import HoverTool, ColumnDataSource, FixedTicker\n",
    "from bokeh.palettes import Category10, Set1, Spectral\n",
    "\n",
    "output_notebook()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def assign_label(rings):\n",
    "    '''\n",
    "    assign string labels to a list of ints\n",
    "    matching the usual zeolite pore size labels\n",
    "    '''\n",
    "\n",
    "    if max(rings) == 8:\n",
    "        return 's'\n",
    "    elif max(rings) == 10:\n",
    "        if 8 in rings:\n",
    "            return 'ms'\n",
    "        else:\n",
    "            return 'm'\n",
    "    elif max(rings) == 12:\n",
    "        if 10 in rings and 8 in rings:\n",
    "            return 'lms'\n",
    "        elif 10 in rings:\n",
    "            return 'lm'\n",
    "        elif 8 in rings:\n",
    "            return 'ls'\n",
    "        else:\n",
    "            return 'l'\n",
    "    elif max(rings) > 12:\n",
    "        return 'x'\n",
    "    else:\n",
    "        return None\n",
    "    \n",
    "def are_all_even(lst):\n",
    "    'check if all the elements of a list are even numbers'\n",
    "\n",
    "    return all(x % 2 == 0 for x in lst)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from zefram import get_session, get_table, framework, Framework, RingSize\n",
    "\n",
    "df = get_table('frameworks')\n",
    "\n",
    "df.loc[:, 'Rings'] = [[r.size for r in framework(row['code']).ring_sizes] for i, row in df.iterrows()]\n",
    "# get the largest ring\n",
    "df.loc[:, 'max_ring'] = df['Rings'].apply(max)\n",
    "# assign a string label\n",
    "df.loc[:, 'ring_label'] = df.Rings.apply(assign_label)\n",
    "df.loc[:, 'alpo_possible'] = df.Rings.apply(are_all_even)\n",
    "df.rename(columns={'lcd': 'Largest cavity dia.', 'pld': 'Pore limiting dia.',\n",
    "                   'maxdsi': 'Max dia. of inc. sphere'}, inplace=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Select the frameworks based on the ranges of parameters\n",
    "\n",
    "- pore limiting diameter in the range [3.5, 4.5]\n",
    "- largest cavity diameter in the range [5.5, 6.5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>code</th>\n",
       "      <th>Pore limiting dia.</th>\n",
       "      <th>Largest cavity dia.</th>\n",
       "      <th>ring_label</th>\n",
       "      <th>Rings</th>\n",
       "      <th>alpo_possible</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>AFN</td>\n",
       "      <td>3.7</td>\n",
       "      <td>5.9</td>\n",
       "      <td>s</td>\n",
       "      <td>[8, 6, 4]</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>APD</td>\n",
       "      <td>3.8</td>\n",
       "      <td>5.6</td>\n",
       "      <td>s</td>\n",
       "      <td>[8, 6, 4]</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>ATT</td>\n",
       "      <td>4.3</td>\n",
       "      <td>6.1</td>\n",
       "      <td>s</td>\n",
       "      <td>[8, 6, 4]</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>AWO</td>\n",
       "      <td>4.3</td>\n",
       "      <td>5.8</td>\n",
       "      <td>s</td>\n",
       "      <td>[8, 6, 4]</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>34</th>\n",
       "      <td>BOF</td>\n",
       "      <td>4.3</td>\n",
       "      <td>6.2</td>\n",
       "      <td>m</td>\n",
       "      <td>[10, 6, 5, 4]</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>38</th>\n",
       "      <td>BRE</td>\n",
       "      <td>3.5</td>\n",
       "      <td>5.9</td>\n",
       "      <td>s</td>\n",
       "      <td>[8, 6, 5, 4]</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>41</th>\n",
       "      <td>CAS</td>\n",
       "      <td>3.5</td>\n",
       "      <td>5.7</td>\n",
       "      <td>s</td>\n",
       "      <td>[8, 6, 5]</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>42</th>\n",
       "      <td>CDO</td>\n",
       "      <td>3.9</td>\n",
       "      <td>6.4</td>\n",
       "      <td>s</td>\n",
       "      <td>[8, 5]</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>44</th>\n",
       "      <td>CGF</td>\n",
       "      <td>3.5</td>\n",
       "      <td>6.4</td>\n",
       "      <td>ms</td>\n",
       "      <td>[10, 8, 6, 4]</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>55</th>\n",
       "      <td>DFT</td>\n",
       "      <td>4.3</td>\n",
       "      <td>5.7</td>\n",
       "      <td>s</td>\n",
       "      <td>[8, 6, 4]</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>59</th>\n",
       "      <td>EDI</td>\n",
       "      <td>4.1</td>\n",
       "      <td>6.4</td>\n",
       "      <td>s</td>\n",
       "      <td>[8, 4]</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>63</th>\n",
       "      <td>EPI</td>\n",
       "      <td>4.1</td>\n",
       "      <td>6.1</td>\n",
       "      <td>s</td>\n",
       "      <td>[8, 5, 4]</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>74</th>\n",
       "      <td>GIS</td>\n",
       "      <td>3.9</td>\n",
       "      <td>5.6</td>\n",
       "      <td>s</td>\n",
       "      <td>[8, 4]</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>84</th>\n",
       "      <td>IHW</td>\n",
       "      <td>4.0</td>\n",
       "      <td>6.2</td>\n",
       "      <td>s</td>\n",
       "      <td>[8, 6, 5, 4]</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>104</th>\n",
       "      <td>JOZ</td>\n",
       "      <td>3.9</td>\n",
       "      <td>5.6</td>\n",
       "      <td>s</td>\n",
       "      <td>[8, 4, 3]</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>106</th>\n",
       "      <td>JSN</td>\n",
       "      <td>4.0</td>\n",
       "      <td>5.8</td>\n",
       "      <td>s</td>\n",
       "      <td>[8, 6, 4]</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>108</th>\n",
       "      <td>JST</td>\n",
       "      <td>3.8</td>\n",
       "      <td>6.0</td>\n",
       "      <td>m</td>\n",
       "      <td>[10, 6, 3]</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>154</th>\n",
       "      <td>OWE</td>\n",
       "      <td>4.4</td>\n",
       "      <td>6.4</td>\n",
       "      <td>s</td>\n",
       "      <td>[8, 6, 4]</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>158</th>\n",
       "      <td>PHI</td>\n",
       "      <td>4.3</td>\n",
       "      <td>6.0</td>\n",
       "      <td>s</td>\n",
       "      <td>[8, 4]</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>162</th>\n",
       "      <td>PUN</td>\n",
       "      <td>4.2</td>\n",
       "      <td>6.2</td>\n",
       "      <td>lms</td>\n",
       "      <td>[12, 10, 8, 4, 3]</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>192</th>\n",
       "      <td>SIV</td>\n",
       "      <td>4.4</td>\n",
       "      <td>6.0</td>\n",
       "      <td>s</td>\n",
       "      <td>[8, 4]</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>195</th>\n",
       "      <td>SOS</td>\n",
       "      <td>4.2</td>\n",
       "      <td>5.5</td>\n",
       "      <td>ls</td>\n",
       "      <td>[12, 8, 6, 4, 3]</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>203</th>\n",
       "      <td>STW</td>\n",
       "      <td>4.1</td>\n",
       "      <td>6.1</td>\n",
       "      <td>ms</td>\n",
       "      <td>[10, 8, 5, 4]</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>208</th>\n",
       "      <td>THO</td>\n",
       "      <td>4.1</td>\n",
       "      <td>5.8</td>\n",
       "      <td>s</td>\n",
       "      <td>[8, 4]</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>213</th>\n",
       "      <td>UEI</td>\n",
       "      <td>4.4</td>\n",
       "      <td>6.2</td>\n",
       "      <td>s</td>\n",
       "      <td>[8, 6, 4]</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    code  Pore limiting dia.  Largest cavity dia. ring_label  \\\n",
       "8    AFN                 3.7                  5.9          s   \n",
       "19   APD                 3.8                  5.6          s   \n",
       "25   ATT                 4.3                  6.1          s   \n",
       "28   AWO                 4.3                  5.8          s   \n",
       "34   BOF                 4.3                  6.2          m   \n",
       "38   BRE                 3.5                  5.9          s   \n",
       "41   CAS                 3.5                  5.7          s   \n",
       "42   CDO                 3.9                  6.4          s   \n",
       "44   CGF                 3.5                  6.4         ms   \n",
       "55   DFT                 4.3                  5.7          s   \n",
       "59   EDI                 4.1                  6.4          s   \n",
       "63   EPI                 4.1                  6.1          s   \n",
       "74   GIS                 3.9                  5.6          s   \n",
       "84   IHW                 4.0                  6.2          s   \n",
       "104  JOZ                 3.9                  5.6          s   \n",
       "106  JSN                 4.0                  5.8          s   \n",
       "108  JST                 3.8                  6.0          m   \n",
       "154  OWE                 4.4                  6.4          s   \n",
       "158  PHI                 4.3                  6.0          s   \n",
       "162  PUN                 4.2                  6.2        lms   \n",
       "192  SIV                 4.4                  6.0          s   \n",
       "195  SOS                 4.2                  5.5         ls   \n",
       "203  STW                 4.1                  6.1         ms   \n",
       "208  THO                 4.1                  5.8          s   \n",
       "213  UEI                 4.4                  6.2          s   \n",
       "\n",
       "                 Rings alpo_possible  \n",
       "8            [8, 6, 4]          True  \n",
       "19           [8, 6, 4]          True  \n",
       "25           [8, 6, 4]          True  \n",
       "28           [8, 6, 4]          True  \n",
       "34       [10, 6, 5, 4]         False  \n",
       "38        [8, 6, 5, 4]         False  \n",
       "41           [8, 6, 5]         False  \n",
       "42              [8, 5]         False  \n",
       "44       [10, 8, 6, 4]          True  \n",
       "55           [8, 6, 4]          True  \n",
       "59              [8, 4]          True  \n",
       "63           [8, 5, 4]         False  \n",
       "74              [8, 4]          True  \n",
       "84        [8, 6, 5, 4]         False  \n",
       "104          [8, 4, 3]         False  \n",
       "106          [8, 6, 4]          True  \n",
       "108         [10, 6, 3]         False  \n",
       "154          [8, 6, 4]          True  \n",
       "158             [8, 4]          True  \n",
       "162  [12, 10, 8, 4, 3]         False  \n",
       "192             [8, 4]          True  \n",
       "195   [12, 8, 6, 4, 3]         False  \n",
       "203      [10, 8, 5, 4]         False  \n",
       "208             [8, 4]          True  \n",
       "213          [8, 6, 4]          True  "
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "criteria = {'Pore limiting dia.': [3.5, 4.5],\n",
    "            'Largest cavity dia.':  [5.5, 6.5]}\n",
    "\n",
    "masks = {}\n",
    "for k, v in criteria.items():\n",
    "    vmin, vmax = sorted(v)\n",
    "    masks[k] = (df[k] >= vmin) & (df[k] < vmax)\n",
    "    \n",
    "mask = pd.concat(list(masks.values()), axis=1).all(axis=1)\n",
    "df.loc[mask, ['code', 'Pore limiting dia.','Largest cavity dia.', 'ring_label', 'Rings', 'alpo_possible']]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "### tighter criteria\n",
    "\n",
    "- pore limiting diameter in the range [3.7, 4.3]\n",
    "- largest cavity diameter in the range [5.7, 6.3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>code</th>\n",
       "      <th>Pore limiting dia.</th>\n",
       "      <th>Largest cavity dia.</th>\n",
       "      <th>ring_label</th>\n",
       "      <th>Rings</th>\n",
       "      <th>alpo_possible</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>AFN</td>\n",
       "      <td>3.7</td>\n",
       "      <td>5.9</td>\n",
       "      <td>s</td>\n",
       "      <td>[8, 6, 4]</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>63</th>\n",
       "      <td>EPI</td>\n",
       "      <td>4.1</td>\n",
       "      <td>6.1</td>\n",
       "      <td>s</td>\n",
       "      <td>[8, 5, 4]</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>84</th>\n",
       "      <td>IHW</td>\n",
       "      <td>4.0</td>\n",
       "      <td>6.2</td>\n",
       "      <td>s</td>\n",
       "      <td>[8, 6, 5, 4]</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>106</th>\n",
       "      <td>JSN</td>\n",
       "      <td>4.0</td>\n",
       "      <td>5.8</td>\n",
       "      <td>s</td>\n",
       "      <td>[8, 6, 4]</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>108</th>\n",
       "      <td>JST</td>\n",
       "      <td>3.8</td>\n",
       "      <td>6.0</td>\n",
       "      <td>m</td>\n",
       "      <td>[10, 6, 3]</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>162</th>\n",
       "      <td>PUN</td>\n",
       "      <td>4.2</td>\n",
       "      <td>6.2</td>\n",
       "      <td>lms</td>\n",
       "      <td>[12, 10, 8, 4, 3]</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>203</th>\n",
       "      <td>STW</td>\n",
       "      <td>4.1</td>\n",
       "      <td>6.1</td>\n",
       "      <td>ms</td>\n",
       "      <td>[10, 8, 5, 4]</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>208</th>\n",
       "      <td>THO</td>\n",
       "      <td>4.1</td>\n",
       "      <td>5.8</td>\n",
       "      <td>s</td>\n",
       "      <td>[8, 4]</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    code  Pore limiting dia.  Largest cavity dia. ring_label  \\\n",
       "8    AFN                 3.7                  5.9          s   \n",
       "63   EPI                 4.1                  6.1          s   \n",
       "84   IHW                 4.0                  6.2          s   \n",
       "106  JSN                 4.0                  5.8          s   \n",
       "108  JST                 3.8                  6.0          m   \n",
       "162  PUN                 4.2                  6.2        lms   \n",
       "203  STW                 4.1                  6.1         ms   \n",
       "208  THO                 4.1                  5.8          s   \n",
       "\n",
       "                 Rings alpo_possible  \n",
       "8            [8, 6, 4]          True  \n",
       "63           [8, 5, 4]         False  \n",
       "84        [8, 6, 5, 4]         False  \n",
       "106          [8, 6, 4]          True  \n",
       "108         [10, 6, 3]         False  \n",
       "162  [12, 10, 8, 4, 3]         False  \n",
       "203      [10, 8, 5, 4]         False  \n",
       "208             [8, 4]          True  "
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "criteria = {'Pore limiting dia.': [3.7, 4.3],\n",
    "            'Largest cavity dia.':  [5.7, 6.3]}\n",
    "\n",
    "masks = {}\n",
    "for k, v in criteria.items():\n",
    "    vmin, vmax = sorted(v)\n",
    "    masks[k] = (df[k] >= vmin) & (df[k] < vmax)\n",
    "    \n",
    "mask = pd.concat(list(masks.values()), axis=1).all(axis=1)\n",
    "df.loc[mask, ['code', 'Pore limiting dia.','Largest cavity dia.', 'ring_label', 'Rings', 'alpo_possible']]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "### Max diameter of a sphere \n",
    "\n",
    "- pore limiting diameter in the range [3.7, 4.3]\n",
    "- maximum diameter of a sphere that can be included in the range [5.7, 6.3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>code</th>\n",
       "      <th>Pore limiting dia.</th>\n",
       "      <th>Largest cavity dia.</th>\n",
       "      <th>Max dia. of inc. sphere</th>\n",
       "      <th>ring_label</th>\n",
       "      <th>Rings</th>\n",
       "      <th>alpo_possible</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>42</th>\n",
       "      <td>CDO</td>\n",
       "      <td>3.9</td>\n",
       "      <td>6.4</td>\n",
       "      <td>5.78</td>\n",
       "      <td>s</td>\n",
       "      <td>[8, 5]</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>59</th>\n",
       "      <td>EDI</td>\n",
       "      <td>4.1</td>\n",
       "      <td>6.4</td>\n",
       "      <td>5.72</td>\n",
       "      <td>s</td>\n",
       "      <td>[8, 4]</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>65</th>\n",
       "      <td>ESV</td>\n",
       "      <td>3.7</td>\n",
       "      <td>6.9</td>\n",
       "      <td>6.22</td>\n",
       "      <td>s</td>\n",
       "      <td>[8, 6, 5, 4]</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>153</th>\n",
       "      <td>OSO</td>\n",
       "      <td>4.2</td>\n",
       "      <td>6.7</td>\n",
       "      <td>6.07</td>\n",
       "      <td>x</td>\n",
       "      <td>[14, 8, 3]</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>215</th>\n",
       "      <td>UOS</td>\n",
       "      <td>4.2</td>\n",
       "      <td>6.5</td>\n",
       "      <td>5.85</td>\n",
       "      <td>ms</td>\n",
       "      <td>[10, 8, 6, 5, 4]</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>228</th>\n",
       "      <td>ZON</td>\n",
       "      <td>4.0</td>\n",
       "      <td>6.5</td>\n",
       "      <td>5.83</td>\n",
       "      <td>s</td>\n",
       "      <td>[8, 6, 4]</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    code  Pore limiting dia.  Largest cavity dia.  Max dia. of inc. sphere  \\\n",
       "42   CDO                 3.9                  6.4                     5.78   \n",
       "59   EDI                 4.1                  6.4                     5.72   \n",
       "65   ESV                 3.7                  6.9                     6.22   \n",
       "153  OSO                 4.2                  6.7                     6.07   \n",
       "215  UOS                 4.2                  6.5                     5.85   \n",
       "228  ZON                 4.0                  6.5                     5.83   \n",
       "\n",
       "    ring_label             Rings alpo_possible  \n",
       "42           s            [8, 5]         False  \n",
       "59           s            [8, 4]          True  \n",
       "65           s      [8, 6, 5, 4]         False  \n",
       "153          x        [14, 8, 3]         False  \n",
       "215         ms  [10, 8, 6, 5, 4]         False  \n",
       "228          s         [8, 6, 4]          True  "
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "criteria = {'Pore limiting dia.': [3.7, 4.3],\n",
    "            'Max dia. of inc. sphere':  [5.7, 6.3]}\n",
    "\n",
    "masks = {}\n",
    "for k, v in criteria.items():\n",
    "    vmin, vmax = sorted(v)\n",
    "    masks[k] = (df[k] >= vmin) & (df[k] < vmax)\n",
    "    \n",
    "mask = pd.concat(list(masks.values()), axis=1).all(axis=1)\n",
    "df.loc[mask, ['code', 'Pore limiting dia.', 'Largest cavity dia.',\n",
    "              'Max dia. of inc. sphere', 'ring_label', 'Rings', 'alpo_possible']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
